package com.appapi.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.appapi.controller.TozhtcController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.Duration;
import java.util.*;

public class Test {
    private Logger logger = LoggerFactory.getLogger(TozhtcController.class);
    public static  void main(String[] args) throws Exception {
         StringRedisTemplate lockRedisTemplate = new StringRedisTemplate();
        boolean fal = lockRedisTemplate.opsForValue().setIfAbsent("LOCK_PREFIX + key", System.currentTimeMillis() + "", Duration.ofSeconds(100));
        System.out.println(fal);
//        downHandler21();

    }
    public static void downHandler21() throws Exception{
        Map parm = new HashMap();
        parm.put("parkCode","2KPI5T73");
        String params = JSON.toJSONString(parm);
        String data= downHandler2("https://openapi-test.yidianting.xin/openydt/api/v2/","3nf1zz","g0qdly2cw9obvs82ywzxsv5pmmsrz72a","getALLParkInfo",params);
//        System.out.println(data);
//        Class<?> clazz = Class.forName("com.appapi.util.Test");
//        try {
//            Method method = clazz.getMethod("downHandler21",String.class,String.class,String.class,String.class,String.class);
//            Object obj1 = method.invoke(clazz.newInstance(),"https://openapi-test.yidianting.xin/openydt/api/v2/","3nf1zz","g0qdly2cw9obvs82ywzxsv5pmmsrz72a","getALLParkInfo",params);
//        }catch (Exception e){
//            data= downHandler2("https://openapi-test.yidianting.xin/openydt/api/v2/","3nf1zz","g0qdly2cw9obvs82ywzxsv5pmmsrz72a","getALLParkInfo",params);
//        }
    }
    public static String downHandler2(String url, String key, String secret, String cmd, String jsonStr) {
        String date = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        String sign = SecureUtil.md5(key + ":" + date + ":" + secret);
        sign = sign.toLowerCase();
        System.out.println(date);
        String aa = "dfjhjhhhKHKHKdfdfKHKHHHdsfdsfjksd";
        List<Integer> list = new ArrayList<>();
        for (int i =0;i<500000;i++){
           list.add(i);
        }
        String date1 = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        System.out.println(date1);
        String auth = Base64.encode(key + ":" + date1);
        System.out.println(list);
        int j = 0;
        for (int i :list
             ) {
            j++;
        }
        System.out.println(j);
        String auth1 = Base64.decodeStr(auth);
        System.out.println("sign:"+sign);
        System.out.println("auth1:"+auth1);
        String resultStr =
                HttpRequest.post(url + cmd + "?sign=" + sign)
                        .header("Accept", "application/json")
                        .header("Content-Type", "application/json;charset=utf-8")
                        .header("Authorization", auth)
                        .body(jsonStr)
                        .execute().body();
        System.out.println("resultStr: " + resultStr);

        return resultStr;
    }
    public static String getCommonResponse(Boolean success) {
        Map<String, Object> params = new HashMap<>();
        params.put("status", 1);
        params.put("message", "业务成功");

        if (success) {
            params.put("resultCode", 0);
        } else {
            params.put("resultCode", -1);
            params.put("data", new JSONObject());
        }

        return JSONObject.toJSONString(params);
    }
}
